<?php 

namespace app\admin\service;

use app\admin\model\AppVersion;

class AppVersionService extends BaseService
{
    public function __construct()
    {
        $this->model = new AppVersion();
    }

    public function getList(): array
    {
        return parent::getList(null, 'newest desc,id desc');
    }

    public function edit()
    {
        $this->model->where('newest',1)->update(['newest' => 0]);
        return parent::edit();
    }

    public function newest(): ?AppVersion
    {
        return $this->model
            ->field('apk, version, describe')
            ->where('mark', 1)
            ->where('newest', 1)
            ->find();
    }

    public function autoConfig()
    {
        $data = db('app_config')->find();
        if ($data) {
            $data['week'] = json_decode($data['week'], true);
        }
        return $data;
    }

    public function saveAutoConfig($data)
    {
        return db('app_config')->where('id', $data['id'])->update($data);
    }
}